﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Aga.GestionDeArchivos.Services;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using Aga.GestionDeArchivos.Domain.Entities;

namespace Aga.GestionDeArchivos.Web.Models.Roles
{
    public class ActionsModel : BaseModel
    {
        private static List<ActionValue> allActions;

        /// <summary>
        /// Initializes the <see cref="ActionsModel"/> class.
        /// </summary>
        static ActionsModel()
        {
            SetAllActions();
        }

        /// <summary>
        /// Gets or sets the role id.
        /// </summary>
        /// <value>The role id.</value>
        public int RoleId { get; set; }

        /// <summary>
        /// Gets or sets the name of the role.
        /// </summary>
        /// <value>The name of the role.</value>
        [Display(Name = "Nombre")]
        public string RoleName { get; set; }

        /// <summary>
        /// Gets or sets the actions.
        /// </summary>
        /// <value>The actions.</value>
        public List<RoleAction> Actions { get; set; }

        /// <summary>
        /// Gets or sets all actions.
        /// </summary>
        /// <value>All actions.</value>
        public static List<ActionValue> GetAllActions()
        {
            return allActions;
        }

        /// <summary>
        /// Sets all actions.
        /// </summary>
        private static void SetAllActions()
        {
            Type type = typeof(Actions);

            allActions = new List<ActionValue>();

            foreach (var action in Enum.GetValues(type))
            {
                if (0 == (int)action)
                    continue;

                FieldInfo fieldInfo = type.GetField(action.ToString());

                var attributes = fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false);

                DisplayAttribute display = attributes[0] as DisplayAttribute;

                allActions.Add(new ActionValue
                {
                    ActionId = (int)action,
                    Name = display.Name,
                    Group = display.GroupName
                });

            }
        }
    }

    public class ActionValue
    {
        public int ActionId { get; set; }
        public string Group { get; set; }
        public string Name { get; set; }
    }
}